/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.sms.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.SmsUtils;
import com.longview.io.common.utils.StringUtils;

import com.longview.io.modules.sms.entity.SmsInfo;
import com.longview.io.modules.sms.service.SmsInfoService;

/**
 * 短信验证码Controller
 * @author 张飞
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsInfo")
public class SmsInfoController extends BaseController {

	@Autowired
	private SmsInfoService smsInfoService;
	
	@ModelAttribute
	public SmsInfo get(@RequestParam(required=false) String id) {
		SmsInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsInfoService.get(id);
		}
		if (entity == null){
			entity = new SmsInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(SmsInfo smsInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsInfo> page = smsInfoService.findPage(new Page<SmsInfo>(request, response), smsInfo); 
		model.addAttribute("page", page);
		return "modules/sms/smsInfoList";
	}

	@RequiresPermissions("sms:smsInfo:view")
	@RequestMapping(value = "form")
	public String form(SmsInfo smsInfo, Model model) {
		model.addAttribute("smsInfo", smsInfo);
		return "modules/sms/smsInfoForm";
	}

	@RequiresPermissions("sms:smsInfo:edit")
	@RequestMapping(value = "save")
	public String save(SmsInfo smsInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsInfo)){
			return form(smsInfo, model);
		}
		smsInfoService.save(smsInfo);
		addMessage(redirectAttributes, "保存短信验证码成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsInfo/?repage";
	}
	
	@RequiresPermissions("sms:smsInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsInfo smsInfo, RedirectAttributes redirectAttributes) {
		smsInfoService.delete(smsInfo);
		addMessage(redirectAttributes, "删除短信验证码成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsInfo/?repage";
	}

	/****
	 * 短信验证码验证
	 */
	
	@RequestMapping(value = "validateSmsCode")
	@ResponseBody
	public String validateSmsCode(HttpServletRequest request,HttpServletResponse response,SmsInfo smsInfo) {
		
		Map<String,String> returnmap = new HashMap<String,String>();
		HttpSession session = request.getSession(true);
		//通过手机号和验证码查询是否成功
		//判断验证码
		String mobile = smsInfo.getMobile().trim();
		String vCode =smsInfo.getVCode().trim();
		//数据为1的时候进行验证码验证
		int ret = cvCodeVerification(mobile,vCode,session,smsInfoService);
		if(ret == 2)
		{
			returnmap.put("code", "2004");
			returnmap.put("msg", "无效验证码");
			return renderString(response, returnmap);
		}
		else if(ret == 1)
		{
			returnmap.put("code", "2014");
			returnmap.put("msg", "验证码错误次数超限,弹出验证吗框");	
			return renderString(response, returnmap);
		}		


		returnmap.put("code", "0000");
		returnmap.put("msg", "验证成功");			
		return renderString(response, returnmap);
		
	}
	/***
	 * 短信发送
	 */
	@RequestMapping(value = "sendSms")
	@ResponseBody
	public String sendSms(HttpServletResponse response,SmsInfo smsInfo) {
		Map<String,String> returnmap = new HashMap<String,String>();
        try {
        	int ret = SmsUtils.SmsSend(smsInfo);
        	if(ret == 0)
        	{
        		returnmap.put("code", "0000");
        		returnmap.put("msg", "发送成功");
        	}
        	else
        	{
        		returnmap.put("code", "1000");
        		returnmap.put("msg", "发送失败");      		
        	}
            return renderString(response, returnmap);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            returnmap.clear();
            returnmap.put("code","-1");
            returnmap.put("msg","短信发送异常");
            return renderString(response, returnmap);
        }
	}
}